package com.itheima;

import java.util.ArrayList;
import java.util.Scanner;

public class MenuDemo {
    public static void main(String[] args) {
        ArrayList<Student> studentList = new ArrayList<>(); // 初始化studentList
        Scanner sc = new Scanner(System.in); // 将Scanner初始化移到studentList之后

        while (true) {
            // 菜单界面代码保持不变...
            System.out.println("-----欢迎使用学生管理系统-----");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出系统");
            System.out.print("请输入您的选择：");

            int choice;
            if (sc.hasNextInt()) { // 检查输入是否为整数
                choice = sc.nextInt();
            } else {
                System.out.println("输入错误，请输入数字选项！");
                sc.nextLine(); // 清除无效输入
                continue; // 跳过本次循环
            }

            // 处理用户选择的代码保持不变...
            switch (choice) {
                case 1:
                    addStudent(studentList, sc);
                    break;
                case 2:
                    deleteStudent(studentList, sc);
                    break;
                case 3:
                    updateStudent(studentList, sc);
                    break;
                case 4:
                    queryStudent(studentList);
                    break;
                case 5:
                    System.out.println("感谢您的使用，再见！");
                    sc.close(); // 关闭Scanner
                    System.exit(0);
                default:
                    System.out.println("输入错误，请重新输入！");
            }
        }
    }

    //增加学生
    public static void addStudent(ArrayList<Student> list, Scanner sc) {
        System.out.println("请输入学生学号：");
        String id = sc.next();
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入学生居住地：");
        String address = sc.next();

        Student student = new Student(id, name, age, address);
        list.add(student);
        System.out.println("添加学生成功");
    }

    // 查询学生方法
    public static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("无信息，请先添加信息再查询！");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t居住地");
        for (Student student : list) {
            System.out.println(student.getId() + "\t\t" + student.getName() + "\t" + student.getAge() + "岁\t" + student.getAddress());
        }
    }

    // 获取学生在集合中的索引方法
    public static int getIndex(ArrayList<Student> list, String sid) {
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getId().equals(sid)) {
                return i;
            }
        }
        return -1;
    }

    // 删除学生方法
    public static void deleteStudent(ArrayList<Student> list, Scanner sc) {
        System.out.println("请输入你要删除的学生的学号：");
        String id = sc.next();
        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("该信息不存在，请重新输入！");
        } else {
            list.remove(index);
            System.out.println("删除学生成功");
        }
    }

    // 修改学生方法
    public static void updateStudent(ArrayList<Student> list, Scanner sc) {
        System.out.println("请输入你要修改的学生的学号：");
        String id = sc.next();
        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("该信息不存在，请重新输入！");
        } else {
            System.out.println("请输入学生新姓名：");
            String name = sc.next();
            System.out.println("请输入学生新年龄：");
            int age = sc.nextInt();
            System.out.println("请输入学生新居住地：");
            String address = sc.next();

            Student student = list.get(index);
            student.setName(name);
            student.setAge(age);
            student.setAddress(address);

            System.out.println("修改学生成功");
        }
    }
}
